<?php

/**
 * Lookup table maintainence controller.
 *
 */
class Lookup_Controller extends SPF_Controller {

   /**
    * The version number of the controller.
    * @var     string
    */
   const VERSION = '1.0.0';

   /**
    * The author of the controller.
    * @var     string
    */
   const AUTHOR  = 'Simon Downes';
   
   /**
    * Array of table names that can be managed by this controller.
    * @var     string
    */
   private $lookups = array('ticket_status', 'ticket_type', 'ticket_priority', 'ticket_severity');
   
   /**
    * Constructor
    * @return       void
    */
   public function __construct() {
      /* construct the parent object */
      parent::__construct(__CLASS__);
   }

   /**
    * Destructor
    * @access       public
    * @return       void
    */
   public function __destruct() {
   }

   /**
    * Determines if the current user is permitted to access this controller and perform the requested action
    * @return       boolean   true if access is allowed, false otherwise.
    */
   public function authenticate() {
   
      return (SPF::$session->user != NULL);
      
   } // authenticate
   
   /**
    * Lists the entries in the registry
    */
   public function index() {
      
      //$this->browse();
      
   } // index
   
   public function browse( $table_name = NULL ) {
      
      if( !in_array($table_name, $this->lookups) || !SPF::$db->has_table($table_name) ) {
         throw new SPF_Exception("Unknown Table: $table_name");
      }
      
      $this->assign('table_name', $table_name);
      $this->assign('lookup_name', ucwords(str_replace('_', ' ', $table_name)) );
      $this->assign('data', Lookup::browse_data($table_name));
      $this->assign('has_system_type', SPF::$db->has_field($table_name, 'system_type'));
      
      $this->display('browse');
      
   } // browse
   
   public function edit( $table_name = NULL, $id = NULL ) {
      
      if( !in_array($table_name, $this->lookups) || !SPF::$db->has_table($table_name) ) {
         throw new SPF_Exception("Unknown Table: $table_name");
      }
      
      // load the request lookup object
      $lookup = new Lookup($table_name);
      $lookup->fetch( (int) $id );
      
      if( $id && !$lookup->id ) {
         SPF::flash(SiTrack::MSG_NOT_FOUND, 'error');
         SPF::redirect('lookup', 'browse', $table_name);
      }
      
      if( SPF::form_submitted('frm_lookup') ) {
         
         // run html_entity_decode() against each element in the post array to make sure we're not
         // inserting html into the database. this is only an issue when re-saving existing records
         // as when output to the browser the data will have html entities encoded and we don't want
         // to be saving the encoded entities into the database...
         $data = array_map('html_entity_decode', SPF::post());
         $lookup->from_array($data);
         
         $lookup->validate();
         
         // if no errors then assign values to dataobject and update
         if( !$lookup->has_errors() ) {
            $lookup->update(false);
            SPF::flash(SiTrack::MSG_SAVED, 'success');
            SPF::redirect('lookup', 'browse', $table_name);
         }
         
         SPF::flash(SiTrack::MSG_ERRORS, 'error');
         
      } // is_submitted

      $this->assign('table_name', $table_name);
      $this->assign('lookup_name', ucwords(str_replace('_', ' ', $table_name)) );
      
      $this->assign('lookup', $lookup->to_array());
      
      $this->assign('system_types', SPF::$db->get_enums($table_name));
      
      $this->display('edit');

   } // edit
   
   public function delete( $table_name = NULL, $id = NULL ) {
      
      if( !in_array($table_name, $this->lookups) || !SPF::$db->has_table($table_name) ) {
         throw new SPF_Exception("Unknown Table: $table_name");
      }
      
      // load the request object
      if( $id ) {
         $lookup = new SPF_Model($table_name);
         $lookup->delete( (int) $id );
         SPF::flash(SiTrack::MSG_DELETED, 'success');
      }
      
      SPF::redirect('lookup', 'browse', $table_name);

   } // delete
   
} // Lookup_Controller

?>
